package spittr.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelExtensionsKt;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import spittr.Spittle;
import spittr.data.SpittleRepository;
import java.util.*;

@Controller
@RequestMapping("/spittler")
public class SpittleController {
    private SpittleRepository spittleRepository;

    private static final String LONG_MAX_AS_STRING = Long.toString(Long.MAX_VALUE);

    public SpittleController() {
        spittleRepository = null;
    }

    @Autowired
    public SpittleController(SpittleRepository spittleRepository) {
        this.spittleRepository = spittleRepository;
    }

    /**
     * 这个方法 model里放的就是SpringMVC里的  M
     * 如果return的是String值则这个String就会当做view名
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/findSp", method = RequestMethod.GET)
    public String spittle(Model model) {
//        Map<String, List> models = new HashMap<>();
//        models.put("spittleList", spittleRepository.findSpittles(Long.MAX_VALUE, 20));
//        model.addAllAttributes(models);
        model.addAttribute(spittleRepository.findSpittles(Long.MAX_VALUE, 20));
//        return spittleRepository.findSpittles(Long.MAX_VALUE, 20);
        return "spittler";
    }

    /**
     * 测试带参数的 url = /spittler/maxCount
     * 如果return的值不是String值，返回的值则会默认塞到model里，model的key值默认由值类型推断。view值由url决定
     *
     * @param max
     * @param count
     * @return
     */
    @RequestMapping(value = "/maxCount", method = RequestMethod.GET)
    public List<Spittle> spittles(@RequestParam(value = "max", defaultValue = "1234567890123") long max,
                                  @RequestParam(value = "count", defaultValue = "50") int count) {
        return spittleRepository.findSpittles(max, count);
    }

    /**
     * 测试参数作为url一部分的情况(@PathVariable)
     * view值设置为spittle， model塞入一个spittle
     *
     * @param spittleId
     * @param model
     * @return
     */
    @RequestMapping(value = "/{spittleId}", method = RequestMethod.GET)
    public String getSpittleBySpittleId(@PathVariable long spittleId, Model model) {
//        Map<String, Spittle> map = new HashMap<>();
//        map.put("spittle", spittleRepository.findOneSpittle(spittleId));
//        model.addAllAttributes(map);
        model.addAttribute(spittleRepository.findOneSpittle(spittleId));
        return "spittleIdOk";
    }

    /**
     * 测试表单类型请求的返回
     *
     * @return
     */
    @RequestMapping(value = "/registers", method = RequestMethod.GET)
    public String showRegistrationForms() {
        return "registerForm1";
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String showRegistrationForm(Spittle spittle, Model model) {
        model.addAttribute(spittleRepository.save(spittle));
        return "registerForm";
    }
}
